/*
 * jQuery 模态提示组件
 */
(function ($) {
    $.modal = function (options) {
        var plugin = this,
            $container = null,
            defaults = {
                title: "",
                content: "",
                confirm:"",
                tarid: false,
                isshowclosebtn: false,
                closecallback: function () {

                },
                surecallback: function () {

                },
                showcontent: false,
                afteropen: function () {
                }
            },
            // Shorthand variable so that we don't need to call
            // plugin.settings throughout the plugin code
            settings;
        plugin.publicMethods = {};

        plugin.init = function () {

            plugin.settings = settings = $.extend({}, defaults, options);
            if (plugin.settings.tarid) {
                $(document.body).delegate(plugin.settings.tarid, 'click', function () {
                    plugin.curdata = $(this).data();
                    plugin.show();
                });
            }
        };

        plugin.show = function (ele, time) {
            if (plugin.settings.animation) {

                $container = $(tool.getAnimation(plugin.settings.content));
                $container.appendTo($(document.body));
                return false;
            }

            //if()

            if (plugin.settings.showcontent) {
                $container = $(tool.getCommonModal(plugin.settings.title, plugin.settings.content));
                $container.appendTo($(document.body));
                initEvents_two(ele, time);
            } else {
                $container = $(tool.getModalTem(plugin.settings.title, plugin.settings.content, plugin.settings.tarid, plugin.settings.isshowclosebtn,plugin.settings.confirm));
                $container.appendTo($(document.body));
                if(!plugin.settings.isshowclosebtn){
                    $("#btn-ensure").css("width","100%");
                }
                initEvents();
                plugin.settings.afteropen();
                $(tool.getBgTem()).appendTo($(document.body));
                $container.show().addClass('in');
                $(document.body).addClass('modal-open');
            }
        };

        plugin.hide = function (time) {
            if (plugin.settings.animation == 1) {
                $container = $(tool.getAnimation());
                //$container.appendTo($(document.body));
                initEvents_animate(time);
                return false;
            }

            $container.removeClass('in');
            $(document.body).removeClass('modal-open');
            $('.modal-backdrop').remove();
            $container.remove();
        };

        var initEvents = function () {
            $container.delegate('#btn-close', 'click', function () {
                plugin.settings.closecallback();
                plugin.hide();
            }).delegate('#btn-ensure', 'click', function () {
                plugin.settings.surecallback();
                plugin.hide();
            }).delegate('#imgclose', 'click', function () {

                plugin.hide();
            });
        };

        var initEvents_two = function (ele, time) {
            var time = time || 3000;
            if (typeof(ele) == "number") {
                time = ele;
            }
            if (ele != undefined && typeof(ele) != "number") {
                ele.attr("disabled", "true");
            }
            $container.animate({'opacity': 0}, time, function () {
                $('.ymm-not-opened').css('display', 'none');
                if (ele != undefined && typeof(ele) != "number") {
                    ele.removeAttr("disabled");
                } else {
                    return
                }
            });
        };

        var initEvents_animate = function (time) {
            if (time != 0) {
                $container.animate({'opacity': 0}, 3000, function () {
                    $('.zbb-not-mask').css('display', 'none');
                });
                $container.animate({'opacity': 0}, 3000, function () {
                    $('.animation-body').css('display', 'none')
                });
            } else {
                $('.zbb-not-mask').css('display', 'none');
                $('.animation-body').css('display', 'none')
            }

            return false;
        };

        var tool = {
            getAnimation: function (content) {
                var str = "";
                str = '<div class="zbb-not-mask">' +
                    ' <div class="animation-body" style="">' +
                    ' <img class="animationimg2" src="../../image/h5/loading.gif"/>' +
                    ' <span class="zz-content">' + content +
                    ' </span>' +
                    ' </div>' +
                    ' </div>';
                return str;
            },
            getBgTem: function (argument) {
                return '<div class="modal-backdrop fade in"></div>';
            },
            getCommonModal: function (title, content) {
                var str = "";
                str = '<div class="ymm-not-opened">' + content + '</div>';
                return str;
            },
            getModalTem: function (title, noticeContent, id, isshowclosebtn,confirm) {
                var str = "",
                    btntem = '';
                if (isshowclosebtn) {
                    btntem += '<button type="button" class="common-btn-cancel" data-dismiss="modal" id="btn-close">取消</button>';
                }
                if(confirm==""){
                    confirm ="确认";
                }
                str = '<div class="zz-mask">'+'</div>'+
                    '<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">' +
                    '<div class="modal-dialog dzz-modal-dialog">' +
                    '<div class="modal-content dzz-modal-content">' +
                    '<div class="modal-header dzz-modal-header">' +
                    '<p class="h2 text-center">' + title + '</p>' +
                    '</div>' +
                    '<div class="modal-body dzz-modal-body text-center">' + noticeContent +
                    '</div>' +
                    '<div class="modal-footer dzz-modal-footer">' +
                    '<button type="button" class="common-btn-big" data-dismiss="modal" id="btn-ensure">'+confirm+'</button>' +
                    btntem +
                    '</div>' +
                    '</div>' +
                    '</div>' +
                    '</div>';

                return str;

            }

        };
        // =============================================================
        // Private functions
        // =============================================================

        plugin.init();
        return plugin;
    };
    // $.fn.modal = function(options) {
    //   return new $.modal(this, options);
    // };

})(jQuery);
